//************************************************************************
//
//  PIC16F887
//  xtal = 20MHz
//
//  EasyPIC 5 board settings
//  J2 set to pull down
//  J17 set to VCC
//  SW2 pin1 RB0 switch on (up position)
//  SW6 pin4 PORTD LED switch on (to the right)
//
//  EasyPIC 6 board settings
//  J2 set to pull down
//  J17 set to VCC
//  SW2 RB0 switch on (up position)
//  SW9 PORTD LED switch on (to the right)
//
//  This short program is a demo of external interrupt usage
//  there is also debounce to control spurious switch bounce
//  from causing the LEDs to flicker too quickly due to switch bounce
//
//  Pressing button RB0 will result in a logic Hi at RB0/INT which will cause
//  an interrupt, then the condition of RB0/INT is tested to see that it is
//  being pressed for more than 100ms before being accepted as a real button
//  press.
//
//  option register is set to operate on rising edge.
//
//************************************************************************


void interrupt(void){

    if (INTCON.INTF == 1)
       {
        if (Button(&PORTB, 0, 100, 1)){  // introduce debounce
                                         // RB0 100ms active Hi
           PORTD = ~PORTD;               // invert PORTD
           }// end button if
        INTCON.INTF = 0;                 // Clear flag everytime inside INTF
       }//end intcon.intf if
}//end ISR

void main() {

  ANSEL  = 0;
  ANSELH = 0;
  C1ON_bit = 0;
  C2ON_bit = 0;

  OPTION_REG = 0xFF; // default value
 
    TRISD = 0;       // PORTD as output
    PORTD = 0x55;    // Set PORTD to 0101 0101 pattern
    TRISB = 0xFF;    // PORTB as input

    INTCON.INTF = 0;  // Clear interrupt flag prior to enable
    INTCON.INTE = 1;  // enable external interrupts
    INTCON.GIE  = 1;  // enable Global interrupts

do{

   Delay_ms(10); // just an empty loop
                 // to keep code running
   }while(1);
}// main